//引入request.js
import request from "./request";

//封装各种业务请求接口，登录，获取用户列表，文件上传，权限接口。。。。。

//封装登录接口
export function LoginApi(data) {
  return request({
    url: "/login",
    method: "POST", //post,put,delete
    data,
  });
}

//获取权限菜单接口
export function getMenusAPI() {
  return request({
    url: "/menus",
    method: "GET",
  });
}

//获取用户列表接口
export function getUsersListAPI(params) {
  return request({
    url: "/users",
    method: "GET",
    params,
  });
}

//添加用户接口
export function addUserAPI(data) {
  return request({
    url: "/users",
    method: "POST",
    data,
  });
}

//改变用户状态的接口
export function setUserStatusAPI(uid, type) {
  return request({
    url: `users/${uid}/state/${type}`,
    method: "put",
  });
}

//编辑用户
/**
 *
 * @param {string} id 用户id
 * @param {Object} data 用户信息{email,mobile}
 * @returns
 */
export function editUserAPI(id, data) {
  return request({
    url: `users/${id}`,
    method: "put",
    data,
  });
}

//删除接口
export function deleteUserAPI(id) {
  return request({
    url: `users/${id}`,
    method: "delete",
  });
}

//显示角色列表
export function getRoleListAPI() {
  return request({
    url: `/roles`,
    method: "get",
  });
}

//分配用户角色  data {rid}
export function setUserRoleAPI(id, rid) {
  return request({
    url: `users/${id}/role`,
    method: "put",
    data: { rid },
  });
}

//获取权限接口
/**
 *
 * @param {String} type  "type=list 列表|tree 树形，默认是list"
 * @returns
 */
export function getRightsListAPI(type = "list") {
  return request({
    url: `rights/${type}`,
    method: "get",
  });
}

///删除角色指定的权限接口
export function deleteRoleRightsAPI(roleId,rightId) {
  return request({
    url: `roles/${roleId}/rights/${rightId}`,
    method: "delete",
  });
}

//更新角色权限的接口
export function updateRoleRightsAPI(roleId,rids) {
  return request({
    url: `roles/${roleId}/rights`,
    method: "POST",
    data:{rids}
  });
}

//商品列表
export function goodslistapi(params) {
  return request({
    url: '/goods',
    method: 'get',
    params
  })
}

//商品分类列表接口
export function goodsCateListAPI(params) {
  return request({
    url: '/categories',
    method: 'get',
    params
  })
}

//获取参数列表接口
export function getGoodsParamsAPI(cateId,sel='many') {
  return request({
    url: `categories/${cateId}/attributes`,
    method: 'get',
    params:{sel}
  })
}


//添加商品接口
/**
 * 
 * @param {Object} data 
 * @returns 
 */
export function addGoodsAPI(data) {
  return request({
    url: `goods`,
    method: 'post',
    data
  })
}

//添加分类接口
export function addGoodsCateAPI(data) {
  return request({
    url: `categories`,
    method: 'post',
    data
  })
}

//编辑分类
export function editGoodsCateAPI(cateId,cat_name) {
  return request({
    url: `categories/${cateId}`,
    method: 'put',
    data:{cat_name}
  })
}

//更新参数值接口
/**
 * 
 * @param {*} cate_id 
 * @param {*} attrId 
 * @param {*} data {attr_name,attr_sel,attr_vals}
 * @returns 
 */
export function updateGoodsParamsValsAPI(cate_id,attrId,data) {
  return request({
    url: `categories/${cate_id}/attributes/${attrId}`,
    method: 'put',
    data
  })
}

//添加动态参数
/**
 * 
 * @param {String} cat_id   分类id
 * @param {Object} data  {attr_name:参数名称,attr_sel:参数类型}
 * @returns 
 */
export function addGoodsManyParamsAPI(cat_id,data) {
  return request({
    url: `categories/${cat_id}/attributes`,
    method: 'POST',
    data
  })
}

//删除分类参数
export function deleteManParamsAPI(cat_id,attr_id) {
  return request({
    url: `categories/${cat_id}/attributes/${attr_id}`,
    method: 'delete'
  })
}


//订单列表
export function getGoodsOrdersListAPI(params) {
  return request({
    url: `orders`,
    method: 'GET',
    params
  })
}


//数据统计图表
export function getGoodsReportAPI() {
  return request({
    url: `reports/type/1`,
    method: 'GET'
  })
}
